﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArtemisTest
{
    //Methods :
    //  HandleEvent - Gets all scripts with specified tag and fires their OnEvent() method
    //  Update - Gets all scripts and runs their Update() method

    public class ScriptSystem : CSystem
    {
        public ScriptSystem(EntitySystem es) : base(es,null) { }

        public void HandleEvent(ScriptEvent args)
        {
            Dictionary<long, Component> scripts = EntitySystem.Components.GetComponentsFromTag(args.Tag, ComponentType.script);
            if (scripts != null)
            {
                foreach (var entry in scripts)
                {
                    ((Script)entry.Value).OnEvent(args);
                }
            }
        }

        public override void Init()
        {

        }

        public override void PreUpdate(double dt)
        {

        }

        public override void Update(double dt)
        {
            var e = EntitySystem.Components.GetAllComponentsOfType(ComponentType.script);
            foreach (KeyValuePair<long, Component> entry in e)
            {
                ((Script)entry.Value).Update(dt);
            }
        }

        public override void PostUpdate(double dt)
        {

        }
    }
}
